﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using WDA.Core;

namespace Sample
{
   [ServiceContract]
   public interface IMonitorService
   {
      [OperationContract]
      TimeSpan GetAverageExecutionTime();
   }
   [MonitorFacade(typeof(IMonitorService))]   
   public interface IMonitor : IMonitorService
   {
      void RegisterOperationTime(TimeSpan time);
   }
   public class MonitorService : IMonitor
   {
      private long _ticks = 0;
      private long _calls = 0;

      #region IMonitorService Members
      public TimeSpan GetAverageExecutionTime()
      {
         if (_calls == 0)
         {
            return TimeSpan.Zero;
         }
         return new TimeSpan(_ticks / _calls);         
      }
      #endregion

      #region IMonitor Members
      public void RegisterOperationTime(TimeSpan time)
      {
         Interlocked.Add(ref _calls, 1);
         Interlocked.Add(ref _ticks, time.Ticks);
      }
      #endregion
   }
}
